home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / SciAn / src / ScianCurrentFunctions.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  9KB  |  361 lines

  1. /*ScianCurrentFunctions.c
  2.   Eric Pepke
  3.   10 August 1993
  4.  
  5.   Does current functions, i.e., functions that work on individual windows' current objects.
  6. */
  7.  
  8. #include "Scian.h"
  9. #include "ScianTypes.h"
  10. #include "ScianLists.h"
  11. #include "ScianWindows.h"
  12. #include "ScianColors.h"
  13. #include "ScianIDs.h"
  14. #include "ScianObjWindows.h"
  15. #include "ScianButtons.h"
  16. #include "ScianErrors.h"
  17. #include "ScianDraw.h"
  18. #include "ScianControls.h"
  19. #include "ScianArrays.h"
  20. #include "ScianScripts.h"
  21. #include "ScianVisWindows.h"
  22. #include "ScianIcons.h"
  23. #include "ScianEvents.h"
  24. #include "ScianStyle.h"
  25. #include "ScianCurrentFunctions.h"
  26. #include "ScianHelp.h"
  27. #include "ScianFilters.h"
  28. #include "ScianFileSystem.h"
  29. #include "ScianSockets.h"
  30. #include "ScianPointers.h"
  31. #include "ScianMenus.h"
  32. #include "ScianPreferences.h"
  33. #include "ScianFileSystem.h"
  34. #include "ScianAnimation.h"
  35. #include "ScianSymbols.h"
  36.  
  37. ObjPtr currentActionClass;
  38.  
  39. typedef struct
  40.     {
  41.     char *name;        /*Name of function*/
  42.     NameTyp message;    /*Message to send to object*/
  43.     char *buttonHelp;    /*Button help*/
  44.     char *menuHelp;        /*Menu help*/
  45.     char *scriptLine;    /*Line for script*/
  46.     ObjPtr menu;        /*Which menu this routine is in*/
  47.     int menuGroup;        /*Which menu group this function is in*/
  48.     } CurrentFunction;
  49.  
  50. int nCurrentFunctions = 0;    /*# of current functions*/
  51.  
  52. static CurrentFunction *currentFunctions;
  53.  
  54. #ifdef PROTO
  55. int NameToCurrentFunction(char *name)
  56. #else
  57. int NameToCurrentFunction(name)
  58. char *name;
  59. #endif
  60. /*Converts a name to a function number, or -1*/
  61. {
  62.     int funcNum;
  63.  
  64.     /*Search for the named function*/
  65.     for (funcNum = 0; funcNum < nCurrentFunctions; ++funcNum)
  66.     {
  67.     if (0 == strcmp2(name, currentFunctions[funcNum] . name))
  68.     {
  69.         break;
  70.     }
  71.     }
  72.  
  73.     if (funcNum >= nCurrentFunctions)
  74.     {
  75.     /*Not found, too bad.*/
  76.     char nameStr[300];
  77.     sprintf(nameStr, "Internal error: name '%s' not found", name);
  78.     ReportError("NameToCurrentFunction", nameStr);
  79.     return -1;
  80.     }
  81.     return funcNum;
  82. }
  83.  
  84. #ifdef PROTO
  85. Bool CurrentFunctionScriptLine(char *line)
  86. #else
  87. Bool CurrentFunctionScriptLine(line)
  88. char *line;
  89. #endif
  90. /*If s is a script line, finds out if it is a valid script line for a
  91.   current function.  If so, does it and returns true, otherwise does nothing
  92.   and returns false.  Determines whether it is valid by matching the 
  93.   script line*/
  94. {
  95.     int funcNum;
  96.     register char *s, *p, *t;
  97.  
  98.     /*For all possible functions*/
  99.     for (funcNum = 0; funcNum < nCurrentFunctions; ++funcNum)
  100.     {
  101.     /*Try to match script line against script*/
  102.     s = line;
  103.     p = currentFunctions[funcNum] . scriptLine;
  104.  
  105.     if (!p) continue;
  106.  
  107.     SKIPBLANKS(s);
  108.     SKIPBLANKS(p);
  109.  
  110.     while (*p)
  111.     {
  112.         if (isspace(*p))
  113.         {
  114.         SKIPBLANKS(s);
  115.         SKIPBLANKS(p);
  116.         }
  117.         else if (toupper(*p) != toupper(*s))
  118.         {
  119.         /*Failure to match*/
  120.         break;
  121.         }
  122.         else
  123.         {
  124.         ++s;
  125.         ++p;
  126.         }
  127.     }
  128.  
  129.     if (*p == 0)
  130.     {
  131.         ObjPtr list;
  132.         ThingListPtr runner;
  133.         ObjPtr object;
  134.         /*It's a match!  Do this function and return true*/
  135.  
  136.         Log(currentFunctions[funcNum] . scriptLine);
  137.         Log("\n");
  138.  
  139.         object = GetVar((ObjPtr) selWinInfo, CURRENT);
  140.         if (object)
  141.         {
  142.             DeferMessage(object, currentFunctions[funcNum] . message);
  143.         }
  144.  
  145.         return true;
  146.     }
  147.     }
  148.     return false;
  149. }
  150.  
  151. #ifdef PROTO
  152. void DefineCurrentFunction(char *funcName, NameTyp message,
  153.     char *scriptLine,
  154.     char *buttonHelp,
  155.     char *menuHelp, ObjPtr menu, int menuGroup)
  156. #else
  157. void DefineCurrentFunction(funcName, message, scriptLine,
  158.     buttonHelp, menuHelp, menu, menuGroup)
  159. char *funcName;
  160. NameTyp message;
  161. char *scriptLine;
  162. char *buttonHelp;
  163. char *menuHelp;
  164. ObjPtr menu;
  165. int menuGroup;
  166. #endif
  167. /*Defines a current function
  168.     funcName        is the name of the function for the menu and buttons
  169.     message        is a message to do it
  170.     scriptLine        is the script line
  171.     buttonHelp        is the help string for a button, or nothing for no button
  172.     menuHelp        is the help string for a menu
  173.     menu        is which menu to use, or NULLOBJ for none
  174.     menuGroup        is the menu group
  175. */
  176. {
  177.     if (nCurrentFunctions)
  178.     {
  179.     currentFunctions = Realloc(currentFunctions, (nCurrentFunctions + 1) * sizeof(CurrentFunction));
  180.     }
  181.     else
  182.     {
  183.     currentFunctions = Alloc((nCurrentFunctions + 1) * sizeof(CurrentFunction));
  184.     }
  185.  
  186.     if (funcName)
  187.     {
  188.     currentFunctions[nCurrentFunctions] . name = Alloc(strlen(funcName) + 1);
  189.     strcpy(currentFunctions[nCurrentFunctions] . name, funcName);
  190.     }
  191.     else
  192.     {
  193.     currentFunctions[nCurrentFunctions] . name = (char *) 0;
  194.     }
  195.     currentFunctions[nCurrentFunctions] . message = message;
  196.     currentFunctions[nCurrentFunctions] . menu = menu;
  197.     currentFunctions[nCurrentFunctions] . menuGroup = menuGroup;
  198.     if (scriptLine)
  199.     {
  200.     currentFunctions[nCurrentFunctions] . scriptLine = Alloc(strlen(scriptLine) + 1);
  201.     strcpy(currentFunctions[nCurrentFunctions] . scriptLine, scriptLine);
  202.     }
  203.     else
  204.     {
  205.     currentFunctions[nCurrentFunctions] . scriptLine = (char *) 0;
  206.     }
  207.     if (buttonHelp)
  208.     {
  209.     currentFunctions[nCurrentFunctions] . buttonHelp = Alloc(strlen(buttonHelp) + 1);
  210.     strcpy(currentFunctions[nCurrentFunctions] . buttonHelp, buttonHelp);
  211.     }
  212.     else
  213.     {
  214.     currentFunctions[nCurrentFunctions] . buttonHelp = (char *) 0;
  215.     }
  216.     if (menuHelp)
  217.     {
  218.     currentFunctions[nCurrentFunctions] . menuHelp = Alloc(strlen(menuHelp) + 1);
  219.     strcpy(currentFunctions[nCurrentFunctions] . menuHelp, menuHelp);
  220.     }
  221.     else
  222.     {
  223.     currentFunctions[nCurrentFunctions] . menuHelp = (char *) 0;
  224.     }
  225.  
  226.     /*Put it on the appropriate menu*/
  227.     if (menu)
  228.     {
  229.     /*Put it on menu*/
  230.     ObjPtr action;
  231.  
  232.     action = NewAction(funcName, currentActionClass);
  233.     SetVar(action, HELPSTRING, NewString(menuHelp));
  234.     AddMenuItem(menu, action, menuGroup);
  235.     }
  236.  
  237.     ++nCurrentFunctions;
  238. }
  239.  
  240. ObjPtr CurrentAction(action)
  241. ObjPtr action;
  242. /*Does the action method of a current action*/
  243. {
  244.     int whichFunction;
  245.     ObjPtr var;
  246.  
  247.     var = GetVar(action, NAME);
  248.     if (var)
  249.     {
  250.     whichFunction = NameToCurrentFunction(GetString(var));
  251.     if (contextHelp)
  252.     {
  253.         /*Give help on the action*/
  254.         ContextHelp(action);
  255.         contextHelp = false;
  256.         MySetCursor(0);
  257.     }
  258.     else
  259.     {
  260.         /*Log it*/
  261.         if (currentFunctions[whichFunction] . scriptLine)
  262.         {
  263.         Log(currentFunctions[whichFunction] . scriptLine);
  264.         Log("\n");
  265.         }
  266.         /*Do it*/
  267.         if (selWinInfo)
  268.         {
  269.         ObjPtr object;
  270.         object = GetVar((ObjPtr) selWinInfo, CURRENT);
  271.         if (object)
  272.         {
  273.             DeferMessage(object, currentFunctions[whichFunction] . message);
  274.         }
  275.         }
  276.     }
  277.     }
  278.     return ObjTrue;
  279. }
  280.  
  281. static ObjPtr MakeCurrentActionActivated(action)
  282. ObjPtr action;
  283. /*Makes a current action activated*/
  284. {
  285.     ObjPtr allSelected;
  286.     ThingListPtr runner;
  287.     int f;
  288.     ObjPtr var;
  289.  
  290.     var = GetStringVar("MakeCurrentActionActivated", action, NAME);
  291.     if (!var) return ObjFalse;
  292.     f = NameToCurrentFunction(GetString(var));
  293.     if (f < 0) return ObjFalse;
  294.  
  295.     if (selWinInfo)
  296.     {
  297.     ObjPtr object;
  298.     object = GetVar((ObjPtr) selWinInfo, CURRENT);
  299.     if (object && GetMethod(object, currentFunctions[f] . message))
  300.     {
  301.         SetVar(action, ACTIVATED, ObjTrue);
  302.         return ObjTrue;
  303.     }
  304.     }
  305.     SetVar(action, ACTIVATED, ObjFalse);
  306.     return ObjTrue;
  307. }
  308.  
  309. #ifdef PROTO
  310. void InitCurrentFunctions(void)
  311. #else
  312. void InitCurrentFunctions()
  313. #endif
  314. /*Initializes the current function system*/
  315. {
  316.     /*Make the class for current actions*/
  317.     currentActionClass = NewObject(actionClass, 0L);
  318.     AddToReferenceList(currentActionClass);
  319.     SetMethod(currentActionClass, ACTIONMETHOD, CurrentAction);
  320.     SetVar(currentActionClass, ACTIVATED, ObjFalse);
  321.     SetMethod(currentActionClass, ACTIVATED, MakeCurrentActionActivated);
  322.     DeclareDependency(currentActionClass, ACTIVATED, READYTOACTIVATE);
  323.  
  324.     /*Define the actions*/
  325.     DefineCurrentFunction(CF_CUT, CUT, (char *) 0,
  326.     "Pressing this button cuts from the current object and places it on the clipboard.",
  327.     "Choosing this menu item cuts from the current object and places it on the clipboard.",
  328.     textMenu, 1);
  329.  
  330.     DefineCurrentFunction(CF_COPY, COPY, (char *) 0,
  331.     "Pressing this button copies from the current object and places it on the clipboard.",
  332.     "Choosing this menu item copies from the current object and places it on the clipboard.",
  333.     textMenu, 1);
  334.  
  335.     DefineCurrentFunction(CF_PASTE, PASTE, (char *) 0,
  336.     "Pressing this button pastes the contents of the clipboard into the current object.",
  337.     "Choosing this menu pastes the contents of the clipboard into the current object.",
  338.     textMenu, 1);
  339. }
  340.  
  341. #ifdef PROTO
  342. void KillCurrentFunctions(void)
  343. #else
  344. void KillCurrentFunctions()
  345. #endif
  346. /*Kills the current function system*/
  347. {
  348.     int k;
  349.  
  350.     for (k = 0; k < nCurrentFunctions; ++k)
  351.     {
  352.     SAFEFREE(currentFunctions[k] . name);
  353.     SAFEFREE(currentFunctions[k] . scriptLine);
  354.     SAFEFREE(currentFunctions[k] . menuHelp);
  355.     SAFEFREE(currentFunctions[k] . buttonHelp);
  356.     }
  357.     SAFEFREE(currentFunctions);
  358.     RemoveFromReferenceList(currentActionClass);
  359. }
  360.  
  361.